<?php

    require_once "config.php";
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $mold = $_POST['mold'];
        if(!empty($mold) && $mold == 'delete'){
            $id = $_POST['id'];
            $bank_sql = $mysql->query("select * from dyspw_mem_bank where  id = {$id}");
            $bank = $bank_sql->fetch_all(MYSQLI_ASSOC);
            if(empty($bank)){
                $result['status'] = 'n';
                $result['info'] = '未查询到该银行卡信息，请重新提交';
                exit(json_encode($result));
            }
            $del_bank = $mysql->query("DELETE from dyspw_mem_bank where id = {$id}");
            if($del_bank){
                $result['status'] = 'y';
                $result['info'] = '银行卡删除成功，即将跳转';
                exit(json_encode($result));
            }else{
                $result['status'] = 'n';
                $result['info'] = '银行卡删除失败，请重新提交';
                exit(json_encode($result));
            }
        }else{
            $date = array(
                'username'      => $_POST['nickname'],
                'bank_add'      => $_POST['bank_add'],
                'bank_mobile'      => $_POST['mobile'],
                'bank_num'      => $_POST['bank'],
                'bank_name'      => $_POST['bank_name'],
                'u_id'      => $_POST['u_id']
            );
            $id = $_POST['id'];
            if(!empty($id)){
                //编辑
                $bank_sql = $mysql->query("select * from dyspw_mem_bank where  id = {$id}");
                $bank = $bank_sql->fetch_all(MYSQLI_ASSOC);
                if(empty($bank)){
                    $result['status'] = 'n';
                    $result['info'] = '未查询到该银行卡号，请重新提交';
                    exit(json_encode($result));
                }
                $bank_add = $mysql->query("UPDATE dyspw_mem_bank set bank_name='{$date['bank_name']}', username='{$date['username']}', bank_add='{$date['bank_add']}', bank_mobile='{$date['bank_mobile']}', bank_num='{$date['bank_num']}' where id = {$id} ");
            }else{
                //添加
                $bank_sql = $mysql->query("select * from dyspw_mem_bank where bank_num = ".$date['bank_num'] ." and u_id = {$date['u_id']}");
                $bank = $bank_sql->fetch_all(MYSQLI_ASSOC);
                if(!empty($bank)){
                    $result['status'] = 'n';
                    $result['info'] = '该改银行卡号已添加';
                    exit(json_encode($result));
                }
                $time = time();
                $bank_add = $mysql->query("INSERT INTO dyspw_mem_bank (bank_name, username, u_id, bank_add, bank_mobile, bank_num, `time`) VALUES ('{$date['bank_name']}','{$date['username']}',{$date['u_id']},'{$date['bank_add']}','{$date['bank_mobile']}','{$date['bank_num']}','{$time}')");
            }
            if($bank_add){
                $result['status'] = 'y';
                $result['info'] = '银行卡提交成功，即将跳转';
                exit(json_encode($result));
            }else{
                $result['status'] = 'n';
                $result['info'] = '银行卡添加失败，请重新提交';
                exit(json_encode($result));
            }
        }
    };

 ?>
